package com.tp.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.tp.base.BaseService;
import com.tp.base.Paginator;
import com.tp.model.sys.SysOrg;
import com.tp.utils.SQLUtils;
import com.tp.utils.StrUtils;

/**
 * 机构service
 * 
 * @author TP
 *
 */
public class OrgService extends BaseService {

	private static SysOrg so = new SysOrg().dao();

	/**
	 * 分页查询
	 * 
	 * @param model
	 * @param orderBy
	 * @param paginator
	 * @return
	 */
	public Page<SysOrg> getPageByWhere(SysOrg model, String orderBy, Paginator paginator) {
		SQLUtils sql = new SQLUtils(
				"from " + SysOrg.tableName + " t left join " + SysOrg.tableName + " p on t.parent_id = p.id where 1=1");
		if (model.getAttrValues().length != 0) {
			sql.whereLike("t.name", model.getStr("name"));
		}
		if (StrUtils.isEmpty(orderBy)) {
			sql.append(" order by t.id desc");
		} else {
			sql.append(" order by ").append(orderBy);
		}
		Page<SysOrg> page = so.paginate(paginator, "select t.*,ifnull(p.name,'组织机构') as parentName", sql.toString());
		return page;
	}

	/**
	 * 获取机构下拉框
	 * 
	 * @param selected
	 * @return
	 */
	public String selectOrg(Integer selected) {
		return selectOrg(selected, 0);
	}

	/**
	 * 获取机构下拉框
	 * 
	 * @return
	 */
	public String selectOrg(Integer selected, Integer selfId) {
		String where = "where status=1 ";
		if (selfId != null && selfId > 0) {
			where += "and id !=" + selfId;
		}
		List<SysOrg> list = so.findByWhere(where + " order by create_time desc");
		StringBuffer sb = new StringBuffer("");
		if (list != null && list.size() > 0) {
			for (SysOrg org : list) {
				int parentId = org.getParentId();
				if (parentId == 0) {
					int id = org.getId();
					sb.append("<option value=\"");
					sb.append(id);
					sb.append("\"");
					boolean flag = (selected != null && id == selected);
					sb.append(flag ? "selected" : "");
					sb.append(">");
					sb.append(org.getName());
					sb.append("</option>");
					sb.append(getOrgTree(id, selected, 1, list));
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 递归树
	 * 
	 * @return
	 */
	public String getOrgTree(int rootId, Integer selected, int level, List<SysOrg> list) {
		if (list == null || list.size() <= 0) {
			return "";
		}
		// 级别太多了
		if (level >= 20) {
			return "";
		}
		StringBuffer sb = new StringBuffer("");
		for (SysOrg org : list) {
			int parentId = org.getParentId();
			if (parentId == rootId) {
				int id = org.getId();
				sb.append("<option value=\"");
				sb.append(id);
				sb.append("\" ");
				boolean flag = (selected != null && id == selected);
				sb.append(flag ? "selected" : "");
				sb.append(">");
				for (int i = 0; i < level; i++) {
					if (i == (level - 1)) {
						sb.append("&nbsp;|--");
					} else {
						sb.append("&nbsp;&nbsp;&nbsp;");
					}
				}
				sb.append(org.getName());
				sb.append("</option>");
				sb.append(getOrgTree(id, selected, (level + 1), list));
			}
		}
		return sb.toString();
	}

	/**
	 * 获取名称
	 * 
	 * @param model
	 * @return
	 */
	/*
	 * public String getDepName(Integer id) { if (id == null || id == 0) {
	 * return ""; } String name = so.findById(id).getStr("name"); return name; }
	 */
}
